package com.example.administrator.zhongyichou.activity;

import android.content.Intent;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.example.administrator.zhongyichou.adapter.MutualMembersListAdapter;
import com.example.administrator.zhongyichou.base.BaseActivity;
import com.example.administrator.zhongyichou.Bean.BaseEventBean;
import com.example.administrator.zhongyichou.base.MyApplication;
import com.example.administrator.zhongyichou.net.NetConfig;
import com.example.administrator.zhongyichou.net.core.RequestHttpLoader;
import com.example.administrator.zhongyichou.net.protocol.GsonBaseProtocol;
import com.example.administrator.zhongyichou.net.protocol.MutualMemberProtocol;
import com.example.administrator.zhongyichou.utils.ToastUtil;
import com.example.administrator.zhongyichou.net.core.HttpLoader;
import com.example.administrator.zhongyichou.net.listener.ResultListener;
import com.example.administrator.zhongyichou.view.HeaderView;
import com.example.administrator.zhongyichou.view.MyDialog;
import com.example.administrator.zhongyichou.R;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lichenyu on 2017/11/23.
 * 描述：
 */

public class MutualMembersActivity extends BaseActivity<MutualMemberProtocol> implements MutualMembersListAdapter.OnChildClickListener {
    @BindView(R.id.headerview)
    HeaderView headerview;
    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.rl_empty)
    RelativeLayout rlEmpty;
    private MutualMembersListAdapter adapter;
    private MutualMemberProtocol mGsonBaseProtocal;

    @Override
    protected void init() {
        ButterKnife.bind(this);
        initHead();
    }

    @Override
    protected void responseData(MutualMemberProtocol mGsonBaseProtocal) {
        this.mGsonBaseProtocal = mGsonBaseProtocal;
        adapter = new MutualMembersListAdapter(this, mGsonBaseProtocal.getData());
        listview.setAdapter(adapter);
        adapter.setOnChildClickListener(this);
        listview.setEmptyView(rlEmpty);
    }

    @Override
    protected HttpLoader initHttpLoader() {
        RequestHttpLoader requestHttpLoader = new RequestHttpLoader(MutualMemberProtocol.class);
        Map<String, String> map = new HashMap<>();
        map.put("user_id", MyApplication.getUserID());
        requestHttpLoader.setParams(NetConfig.MUTUAL_MEMBER, map);
        return requestHttpLoader;
    }

    @Override
    protected boolean isRequestData() {
        return true;
    }

    @Override
    protected int initContentView() {
        return R.layout.mutual_members_activity;
    }

    @Override
    public boolean onEventBus(BaseEventBean baseEventBean) {

        return false;
    }

    private void initHead() {
        headerview.setTitle("会员卡");
        headerview.bindOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case HeaderView.LEFT:
                        finish();
                        break;
                }
            }
        });
    }

    //充值激活
    @Override
    public void onRecharge(int position) {
        if (mGsonBaseProtocal.getData().get(position).getMember_status().equals("0")) {//跳转激活
            Intent intent = new Intent(this, WaitPayActivity.class);
            intent.putExtra("member_id", mGsonBaseProtocal.getData().get(position).getMember_id());
            startActivity(intent);
        } else {//跳转充值
            Intent intent = new Intent(this, RechargeActivity.class);
            intent.putExtra("member_id", mGsonBaseProtocal.getData().get(position).getMember_id());
            startActivity(intent);
        }
    }

    //删除
    @Override
    public void onDelete(final int position) {
        MyDialog dialog = new MyDialog(this);
        dialog.setTxtContent("确认删除此会员卡？");
        dialog.setOnChekedClick(new MyDialog.OnChekedClick() {
            @Override
            public void onCancelClick() {

            }

            @Override
            public void onConfirmClick() {
                delete(position);
            }
        });
        dialog.show();
    }

    private void delete(int position) {
        RequestHttpLoader requestHttpLoader = new RequestHttpLoader(GsonBaseProtocol.class);
        Map map = new HashMap();
        map.put("user_id", MyApplication.getUserID());
        map.put("member_id", mGsonBaseProtocal.getData().get(position).getMember_id());
        requestHttpLoader.setParams(NetConfig.DELETE_MUTUAL, map);
        requestHttpLoader.showDialog(this, true, "正在删除");
        requestHttpLoader.executor(new ResultListener() {
            @Override
            public void success(GsonBaseProtocol mGsonBaseProtocol) {
                ToastUtil.showToast("删除成功");
                requestData();
            }

            @Override
            public void error(String mErrorProtocal) {

            }

            @Override
            public void notNet() {

            }

            @Override
            public void requestError() {

            }
        });
    }

    //查看凭证
    @Override
    public void onOpen(int position) {
        Intent intent = new Intent(this, MemberDetailsActivity.class);
        intent.putExtra("member_id", mGsonBaseProtocal.getData().get(position).getMember_id());
        startActivity(intent);
    }
}
